<!--柱状图-->
<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
  import echarts from 'echarts'
  require('echarts/theme/macarons')   // echarts theme
  import resize from './../mixins/resize'
  import { listBar } from "@/api/dashboard/barchart";

  const animationDuration = 3000

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '300px'
      }
    },
    data() {
      return {
        MapList:[],
        rows:[],

        barInCount:[],
        barOutCount:[],
        barDate :[],
        chart: null,
        queryParams: {
          barInCount: undefined,
          barOutCount: undefined,
          barDate: undefined,
        },
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    created() {
      this.getBarList();
    },
    methods: {
      getBarList() {
        this.loading = true;
        listBar(this.queryParams).then(response => {
          this.MapList =response.rows;
          console.log( "柱状图数据：" + this.MapList);         // 确认数据有获取到
          let MapList = this.MapList;
          if(MapList){
            let obj = eval(MapList);
            for(let i=0;i<obj.length;i++){
              this.barInCount.push(MapList[i].barInCount);
              this.barOutCount.push(MapList[i].barOutCount);
              this.barDate.push(MapList[i].barDate);
            }
          }
          //动态插入数据时，数据在这里设置更新
          this.chart.setOption({
            xAxis: {
              data: this.barDate
            },
            series: [{
              name: '入库数量',
              data: this.barInCount,
            },
              {
                name: '出库数量',
                data: this.barOutCount,
              }]
          });
        });
      },
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')
        this.chart.setOption({
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            }
          },
          grid: {
            top: 30,
            left: '2%',
            right: '2%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [{
            type: 'category',
            data: this.barDate,
            axisTick: {
              alignWithLabel: true
            }
          }],
          yAxis: [{
            type: 'value',
            axisTick: {
              show: false
            }
          }],
          legend: {
            data: ['入库数量', '出库数量'],
            textStyle:{
              // fontSize: 16,

            }
          },
          series: [{
            name: '入库数量',
            type: 'bar',
            stack: 'Amount',
            barWidth: '10%',
            data: this.barInCount,
            // markPoint: {
            //   data: [
            //     {
            //       type: 'max', name: '最大值'
            //     },
            //     {
            //       type: 'min', name: '最小值'
            //     }
            //   ]
            // },
            markLine: {
              data: [
                {
                  type: 'average', name: '平均值'
                }
              ]
            },
            itemStyle: {
              normal: {
                label: {
                  show: true, //开启显示
                  position: 'top', //在上方显示
                  textStyle: { //数值样式
                    // color: 'white',
                    // fontSize: 16
                  }
                }
              }
            },
            animationDuration
          }, {
            name: '出库数量',
            type: 'bar',
            stack: 'count',
            barWidth: '10%',
            data: this.barOutCount,
            itemStyle: {
              normal: {
                label: {
                  show: true, //开启显示
                  position: 'top', //在上方显示
                  textStyle: { //数值样式
                    // color: 'white',
                    // fontSize: 16
                  }
                }
              }
            },
            animationDuration
          }]
        })
      },

    }
  }
</script>
